package com.jt.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.util.Date;

@Component//将对象交给spring容器管理  因为不属于C/S/M
public class MyMetaObjectHandler implements MetaObjectHandler {

    //入库操作时调用  created/updated
    /*
    * setFieldValByName(1,2,3)
    * 1:自动填充字段名称  2:自动填充的值   3:metaObject固定写法
    * */
    @Override
    public void insertFill(MetaObject metaObject) {
        //设置时间变量
        Date date = new Date();
        this.setFieldValByName("created",date ,metaObject );
        this.setFieldValByName("updated",date ,metaObject );
    }
    //更新操作时调用 updated
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updated",new Date() ,metaObject );
    }
}
